import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class BIABReader {

	static final String rootNames[] = { "C", "Db", "D", "Eb", "E", "F", "Gb",
			"G", "Ab", "A", "Bb", "B", "C#", "D#", "F#", "G#", "A#" };
	static Object typeNames[] = {
		  "",  /* 1 */
		  "Maj",  /* 2 */
		  "b5",  /* 3 */
		  "aug",  /* 4 */
		  "6",  /* 5 */
		  "Maj7",  /* 6 */
		  "Maj9",  /* 7 */
		  "Maj9#11",  /* 8 */
		  "Maj13#11",  /* 9 */
		  "Maj13",  /* 10 */
		  "Maj9(no 3)",  /* 11 */
		  "+",  /* 12 */
		  "Maj7#5",  /* 13 */
		  "69",  /* 14 */
		  "2",  /* 15 */
		  "m",  /* 16 */
		  "maug",  /* 17 */
		  "mMaj7",  /* 18 */
		  "m7",  /* 19 */
		  "m9",  /* 20 */
		  "m11",  /* 21 */
		  "m13",  /* 22 */
		  "m6",  /* 23 */
		  "m#5",  /* 24 */
		  "m7#5",  /* 25 */
		  "?",  /* 26 */
		  "?",  /* 27 */
		  "?",  /* 28 */
		  "?",  /* 29 */
		  "?",  /* 30 */
		  "?",  /* 31 */
		  "m7b5",  /* 32 */
		  "dim",  /* 33 */
		  "?",  /* 34 */
		  "?",  /* 35 */
		  "?",  /* 36 */
		  "?",  /* 37 */
		  "?",  /* 38 */
		  "?",  /* 39 */
		  "5",  /* 40 */
		  "?",  /* 41 */
		  "?",  /* 42 */
		  "?",  /* 43 */
		  "?",  /* 44 */
		  "?",  /* 45 */
		  "?",  /* 46 */
		  "?",  /* 47 */
		  "?",  /* 48 */
		  "?",  /* 49 */
		  "?",  /* 50 */
		  "?",  /* 51 */
		  "?",  /* 52 */
		  "?",  /* 53 */
		  "?",  /* 54 */
		  "?",  /* 55 */
		  "7+",  /* 56 */
		  "9+",  /* 57 */
		  "13+",  /* 58 */
		  "?",  /* 59 */
		  "?",  /* 60 */
		  "?",  /* 61 */
		  "?",  /* 62 */
		  "?",  /* 63 */
		  "7",  /* 64 */
		  "13",  /* 65 */
		  "7b13",  /* 66 */
		  "7#11",  /* 67 */
		  "13#11",  /* 68 */
		  "7#11b13",  /* 69 */
		  "9",  /* 70 */
		  "?",  /* 71 */
		  "9b13",  /* 72 */
		  "9#11",  /* 73 */
		  "13#11",  /* 74 */
		  "9#11b13",  /* 75 */
		  "7b9",  /* 76 */
		  "13b9",  /* 77 */
		  "7b9b13",  /* 78 */
		  "7b9#11",  /* 79 */
		  "13b9#11",  /* 80 */
		  "7b9#11b13",  /* 81 */
		  "7#9",  /* 82 */
		  "13#9",  /* 83 */
		  "7#9b13",  /* 84 */
		  "9#11",  /* 85 */
		  "13#9#11",  /* 86 */
		  "7#9#11b13",  /* 87 */
		  "7b5",  /* 88 */
		  "13b5",  /* 89 */
		  "7b5b13",  /* 90 */
		  "9b5",  /* 91 */
		  "9b5b13",  /* 92 */
		  "7b5b9",  /* 93 */
		  "13b5b9",  /* 94 */
		  "7b5b9b13",  /* 95 */
		  "7b5#9",  /* 96 */
		  "13b5#9",  /* 97 */
		  "7b5#9b13",  /* 98 */
		  "7#5",  /* 99 */
		  "13#5",  /* 100 */
		  "7#5#11",  /* 101 */
		  "13#5#11",  /* 102 */
		  "9#5",  /* 103 */
		  "9#5#11",  /* 104 */
		  "7#5b9",  /* 105 */
		  "13#5b9",  /* 106 */
		  "7#5b9#11",  /* 107 */
		  "13#5b9#11",  /* 108 */
		  "7#5#9",  /* 109 */
		  "13#5#9#11",  /* 110 */
		  "7#5#9#11",  /* 111 */
		  "13#5#9#11",  /* 112 */
		  "7alt",  /* 113 */
		  "?",  /* 114 */
		  "?",  /* 115 */
		  "?",  /* 116 */
		  "?",  /* 117 */
		  "?",  /* 118 */
		  "?",  /* 119 */
		  "?",  /* 120 */
		  "?",  /* 121 */
		  "?",  /* 122 */
		  "?",  /* 123 */
		  "?",  /* 124 */
		  "?",  /* 125 */
		  "?",  /* 126 */
		  "?",  /* 127 */
		  "7sus",  /* 128 */
		  "13sus",  /* 129 */
		  "7susb13",  /* 130 */
		  "7sus#11",  /* 131 */
		  "13sus#11",  /* 132 */
		  "7sus#11b13",  /* 133 */
		  "9sus",  /* 134 */
		  "?",  /* 135 */
		  "9susb13",  /* 136 */
		  "9sus#11",  /* 137 */
		  "13sus#11",  /* 138 */
		  "9sus#11b13",  /* 139 */
		  "7susb9",  /* 140 */
		  "13susb9",  /* 141 */
		  "7susb913",  /* 142 */
		  "7susb9#11",  /* 143 */
		  "13susb9#11",  /* 144 */
		  "7susb9#11b13",  /* 145 */
		  "7sus#9",  /* 146 */
		  "13sus#9",  /* 147 */
		  "7sus#9b13",  /* 148 */
		  "9sus#11",  /* 149 */
		  "13sus#9#11",  /* 150 */
		  "7sus#9#11b13",  /* 151 */
		  "7susb5",  /* 152 */
		  "13susb5",  /* 153 */
		  "7susb5b13",  /* 154 */
		  "9susb5",  /* 155 */
		  "9susb5b13",  /* 156 */
		  "7susb5b9",  /* 157 */
		  "13susb5b9",  /* 158 */
		  "7susb5b9b13",  /* 159 */
		  "7susb5#9",  /* 160 */
		  "13susb5#9",  /* 161 */
		  "7susb5#9b13",  /* 162 */
		  "7sus#5",  /* 163 */
		  "13sus#5",  /* 164 */
		  "7sus#5#11",  /* 165 */
		  "13sus#5#11",  /* 166 */
		  "9sus#5",  /* 167 */
		  "9sus#5#11",  /* 168 */
		  "7sus#5b9",  /* 169 */
		  "13sus#5b9",  /* 170 */
		  "7sus#5b9#11",  /* 171 */
		  "13sus#5b9#11",  /* 172 */
		  "7sus#5#9",  /* 173 */
		  "13sus#5#9#11",  /* 174 */
		  "7sus#5#9#11",  /* 175 */
		  "13sus#5#9#11",  /* 176 */
		  "4",  /* 177 */
		  "?",  /* 178 */
		  "?",  /* 179 */
		  "?",  /* 180 */
		  "?",  /* 181 */
		  "?",  /* 182 */
		  "?",  /* 183 */
		  "sus",  /* 184 */
		  /* no chord names above 185 */
		};

	static final Object typeNamesInt[] = { 
	Constants.Maj, /* 1 */
	Constants.Maj, /* 2 */
	"b5", /* 3 */
	"aug", /* 4 */
	Constants.six, /* 5 */
	Constants.Maj7, /* 6 */
	"Maj9", /* 7 */
	"Maj9#11", /* 8 */
	"Maj13#11", /* 9 */
	"Maj13", /* 10 */
	"Maj9(no 3)", /* 11 */
	Constants.aug, /* 12 */
	"Maj7#5", /* 13 */
	"69", /* 14 */
	"2", /* 15 */
	Constants.m, /* 16 */
	"maug", /* 17 */
	"mMaj7", /* 18 */
	Constants.m7, /* 19 */
	"m9", /* 20 */
	"m11", /* 21 */
	"m13", /* 22 */
	"m6", /* 23 */
	"m#5", /* 24 */
	"m7#5", /* 25 */
	"?", /* 26 */
	"?", /* 27 */
	"?", /* 28 */
	"?", /* 29 */
	"?", /* 30 */
	"?", /* 31 */
	Constants.m7b5, /* 32 */
	Constants.dim, /* 33 */
	"?", /* 34 */
	"?", /* 35 */
	"?", /* 36 */
	"?", /* 37 */
	"?", /* 38 */
	"?", /* 39 */
	"5", /* 40 */
	"?", /* 41 */
	"?", /* 42 */
	"?", /* 43 */
	"?", /* 44 */
	"?", /* 45 */
	"?", /* 46 */
	"?", /* 47 */
	"?", /* 48 */
	"?", /* 49 */
	"?", /* 50 */
	"?", /* 51 */
	"?", /* 52 */
	"?", /* 53 */
	"?", /* 54 */
	"?", /* 55 */
	"7+", /* 56 */
	"9+", /* 57 */
	"13+", /* 58 */
	"?", /* 59 */
	"?", /* 60 */
	"?", /* 61 */
	"?", /* 62 */
	"?", /* 63 */
	Constants.dom7, /* 64 */
	Constants.dom13, /* 65 */
	"7b13", /* 66 */
	"7#11", /* 67 */
	"13#11", /* 68 */
	"7#11b13", /* 69 */
	Constants.Maj9, /* 70 */
	"?", /* 71 */
	"9b13", /* 72 */
	"9#11", /* 73 */
	"13#11", /* 74 */
	"9#11b13", /* 75 */
	Constants.dom7b9, /* 76 */
	Constants.dom13b9, /* 77 */
	"7b9b13", /* 78 */
	"7b9#11", /* 79 */
	"13b9#11", /* 80 */
	"7b9#11b13", /* 81 */
	Constants.dom7s9, /* 82 */
	"13#9", /* 83 */
	"7#9b13", /* 84 */
	Constants.dom79s11, /* 85 */
	"13#9#11", /* 86 */
	"7#9#11b13", /* 87 */
	"7b5", /* 88 */
	"13b5", /* 89 */
	"7b5b13", /* 90 */
	"9b5", /* 91 */
	"9b5b13", /* 92 */
	"7b5b9", /* 93 */
	"13b5b9", /* 94 */
	"7b5b9b13", /* 95 */
	"7b5#9", /* 96 */
	"13b5#9", /* 97 */
	"7b5#9b13", /* 98 */
	"7#5", /* 99 */
	"13#5", /* 100 */
	"7#5#11", /* 101 */
	"13#5#11", /* 102 */
	"9#5", /* 103 */
	"9#5#11", /* 104 */
	"7#5b9", /* 105 */
	"13#5b9", /* 106 */
	"7#5b9#11", /* 107 */
	"13#5b9#11", /* 108 */
	"7#5#9", /* 109 */
	"13#5#9#11", /* 110 */
	"7#5#9#11", /* 111 */
	"13#5#9#11", /* 112 */
	Constants.alt,  /* 113 */
	"?", /* 114 */
	"?", /* 115 */
	"?", /* 116 */
	"?", /* 117 */
	"?", /* 118 */
	"?", /* 119 */
	"?", /* 120 */
	"?", /* 121 */
	"?", /* 122 */
	"?", /* 123 */
	"?", /* 124 */
	"?", /* 125 */
	"?", /* 126 */
	"?", /* 127 */
	"7sus", /* 128 */
	Constants.sus13, /* 129 */
	"7susb13", /* 130 */
	"7sus#11", /* 131 */
	"13sus#11", /* 132 */
	"7sus#11b13", /* 133 */
	"9sus", /* 134 */
	"?", /* 135 */
	"9susb13", /* 136 */
	"9sus#11", /* 137 */
	"13sus#11", /* 138 */
	"9sus#11b13", /* 139 */
	"7susb9", /* 140 */
	"13susb9", /* 141 */
	"7susb913", /* 142 */
	"7susb9#11", /* 143 */
	"13susb9#11", /* 144 */
	"7susb9#11b13", /* 145 */
	"7sus#9", /* 146 */
	"13sus#9", /* 147 */
	"7sus#9b13", /* 148 */
	"9sus#11", /* 149 */
	"13sus#9#11", /* 150 */
	"7sus#9#11b13", /* 151 */
	"7susb5", /* 152 */
	"13susb5", /* 153 */
	"7susb5b13", /* 154 */
	"9susb5", /* 155 */
	"9susb5b13", /* 156 */
	"7susb5b9", /* 157 */
	"13susb5b9", /* 158 */
	"7susb5b9b13", /* 159 */
	"7susb5#9", /* 160 */
	"13susb5#9", /* 161 */
	"7susb5#9b13", /* 162 */
	"7sus#5", /* 163 */
	"13sus#5", /* 164 */
	"7sus#5#11", /* 165 */
	"13sus#5#11", /* 166 */
	"9sus#5", /* 167 */
	"9sus#5#11", /* 168 */
	"7sus#5b9", /* 169 */
	"13sus#5b9", /* 170 */
	"7sus#5b9#11", /* 171 */
	"13sus#5b9#11", /* 172 */
	"7sus#5#9", /* 173 */
	"13sus#5#9#11", /* 174 */
	"7sus#5#9#11", /* 175 */
	"13sus#5#9#11", /* 176 */
	"4", /* 177 */
	"?", /* 178 */
	"?", /* 179 */
	"?", /* 180 */
	"?", /* 181 */
	"?", /* 182 */
	"?", /* 183 */
	"sus", /* 184 */
	/* no chord names above 185 */
	};

	static String name;
	static int meterNumerator;
	static int meterDenominator;
	static String key;
	static boolean isMinor;
	static int numberOfChords = 0;
	static int tempo = 0;
	static char[] types = new char[1020];
	static char[] roots = new char[1020];
	static int[] beats = new int[1020];
	static char songKey;
	static int songKeyInt;

	public static void OutputChord(int beat, int duration, String chord) {
		System.out.println("Beat = " + beat + " Duration = " + duration
				+ " chord = " + chord);
	}

	public static String ChordName(char root, char type) {
		int chordRoot = root % 18;
		int chordBass = (chordRoot - 1 + root / 18) % 12 + 1;
		String string = null;
		if (chordRoot == chordBass)
			string = (String)(rootNames[chordRoot - 1]).concat((String)(typeNames[type - 1]));
		else
			string = String.format("%s%s/%s", rootNames[chordRoot - 1],
					(typeNames[type - 1]), rootNames[chordBass - 1]);
		return string;
	}

	public static void ReadBiaBFileNewFormat(FileInputStream ifs)
			throws IOException {
		int nameLen = ifs.read();
		byte[] byteArray = new byte[nameLen];
		ifs.read(byteArray, 0, nameLen);
		char[] nameChars = new char[nameLen];
		for (int i = 0; i < nameLen; i++) {
			nameChars[i] = (char) (byteArray[i]);
		}
		name = String.valueOf(nameChars);
		// Skip to byte denoting the style.
		ifs.read();
		ifs.read();

		char style = (char) (ifs.read());
		meterNumerator = (style == 8 || style == 17) ? 3 : 4;
		meterDenominator = 4;
		songKey = (char) (ifs.read());
		if (songKey <= 17) {
			key = rootNames[songKey - 1];
			songKeyInt=(int) songKey - 1;
			isMinor = false;
		} else {
			key = rootNames[songKey - 18];
			songKeyInt = songKey - 18;
			isMinor = true;
		}
		// Read tempo.
		char tempo1 = (char) (ifs.read());
		char tempo2 = (char) (ifs.read());
		tempo = tempo2 * 256 + tempo1;

		// This is the starting bar number.
		int bar = ifs.read();



		numberOfChords = 0;

		// Maximum 255 bars!
		while (bar < 255) {
			char barType = (char) (ifs.read());
			if (barType == 0) {
				char duration = (char) (ifs.read());
				bar += duration;
			} else
				bar += 1;
		}

		int beat = 0;
		// Maximum 255 bars!
		while (beat < 1020) {
			char chordType = (char) (ifs.read());
			if (chordType == 0) {
				// handle rests (or more accurately: "space")
				char duration = (char) (ifs.read());
				beat += duration;
			} else {
				types[numberOfChords] = chordType;
				beats[numberOfChords] = beat;
				numberOfChords++;
				beat += 1;
			}
		}

		beat = 0;
		int i = 0;
		// Maximum 255 bars!
		while (beat < 1020) {
			char chordRoot = (char) (ifs.read());
			if (chordRoot == 0) {
				// handle rests (or more accurately: "space")
				char duration = (char) (ifs.read());
				beat += duration;
			} else {
				roots[i] = chordRoot;
				if (beat != beats[i])
					System.out
							.println("Inconsistent chord type and root beat, on beat: "
									+ i);
				i++;
				beat += 1;
			}
		}

		if (i != numberOfChords)
			System.out.println("Inconsistent number of chord types and roots");

		for (int j = 0; j < numberOfChords - 1; j++) {
			String chord = ChordName(roots[j], types[j]);
			int chordBeat = beats[j] * meterNumerator / meterDenominator;
			int chordDuration = (beats[j + 1] - beats[j]) * meterNumerator
					/ meterDenominator;
			System.out.print("Chord Quality = "+(int)(types[j])+" ");
			OutputChord(chordBeat, chordDuration, chord);
		}
		String chord = ChordName(roots[numberOfChords - 1],
				types[numberOfChords - 1]);

		OutputChord(beats[numberOfChords - 1] * meterNumerator
				/ meterDenominator, meterNumerator, chord);
	}

	//
	// void ReadBiaBFileOldFormat(std::ifstream& ifs)
	// {
	// unsigned char nameLen = ifs.get();
	// char nameChars[nameLen];
	// ifs.read(nameChars, nameLen);
	// name = std::string(nameChars, nameLen);
	//
	// // The byte denoting the style of the file is at position 0x3e
	// ifs.seekg(0x3e);
	// unsigned char style = ifs.get();
	// meterNumerator = (style == 8 || style == 17) ? 3 : 4;
	// meterDenominator = 4;
	//
	// unsigned char songKey = ifs.get();
	// if (songKey <= 17) {
	// key = rootNames[songKey - 1];
	// isMinor = false;
	// }
	// else {
	// key = rootNames[songKey - 18];
	// isMinor = true;
	// }
	//
	// // Read tempo.
	// unsigned char tempo1 = ifs.get();
	// unsigned char tempo2 = ifs.get();
	// tempo = tempo2 * 256 + tempo1;
	//
	// // The chord types start at byte 0x82
	// ifs.seekg(0x82);
	//
	// unsigned char types[256];
	// unsigned char roots[256];
	// unsigned int durations[256];
	//
	// for (int i = 0; i < 256; i++)
	// types[i] = ifs.get();
	//
	// for (int i = 0; i < 256; i++)
	// roots[i] = ifs.get();
	//
	// ifs.get();
	// ifs.get();
	// // This seems to be the length of the chorus (for determining the
	// duration of the last chord).
	// unsigned char chorusLength = ifs.get();
	//
	// for (int i = 0; i < chorusLength * 4; i++) {
	// if (types[i] != 0) {
	// unsigned int duration = 1;
	// int j = i + 1;
	// while (j < chorusLength * 4 && types[j] == 0) {
	// j++;
	// duration++;
	// }
	// durations[i] = duration;
	// }
	// }
	//
	// for (int i = 0; i < chorusLength * 4; i++)
	// if (types[i] != 0)
	// OutputChord(i, durations[i] * meterNumerator / meterDenominator,
	// ChordName(roots[i], types[i]));
	// }

	public static void ReadBiaBFile(String filename) throws IOException {
		File file = new File(filename);
		FileInputStream ifs = new FileInputStream(file);
		if (ifs.equals(null))
			System.out
					.println("!!!!!!!!!! - Cannot open file to initialize chord sequence - !!!!!!!!!!!!!!");

		int version = (int) (ifs.read());
		if (version == 0xbb || version == 0xbc) {
			System.out.println("OLD VERSION");
			// ReadBiaBFileOldFormat(ifs);
		} else {
			System.out.println("New format: " + version);
			ReadBiaBFileNewFormat(ifs);
		}
		System.out.println("Name = " + name);
		System.out
				.println("Meter = " + meterNumerator + "/" + meterDenominator);
		System.out.println("Key = " + key + (isMinor ? "m" : ""));
		System.out.println("Tempo = " + tempo);
	}

	@SuppressWarnings("deprecation")
//	public static void main(String args[]) throws IOException {
//		FileDialog fd;
//		Frame f = new Frame();
//
//		// open a MIDI file
//		fd = new FileDialog(f, "Open MIDI file or choose cancel to"
//				+ " finish.", FileDialog.LOAD);
//		fd.show();
//		// break out when user presses cancel
//		if (fd.getFile() != null) {
//			String filename = fd.getDirectory() + fd.getFile();
//			ReadBiaBFile(filename);
//		}
//	}
	
	public static void printMatrix() throws IOException
	{
		FileDialog fd;
		Frame f = new Frame();

		// open a MIDI file
		fd = new FileDialog(f, "Open MIDI file or choose cancel to"
				+ " finish.", FileDialog.LOAD);
		fd.show();
		// break out when user presses cancel
		if (fd.getFile() != null) {
			String filename = fd.getDirectory() + fd.getFile();
			ReadBiaBFile(filename);
		}
//		System.out.print("\t\t");
//		for (int j = 0; j <= numberOfChords - 1; j++) {
//			System.out.print(j+", \t");
//		}		
		System.out.print("\npublic int[][] changes = {\n\t{");
		for (int j = 0; j <= numberOfChords - 1; j++) {
			System.out.print("\t"+((int)(roots[j]%18)-1)+", ");
		}
		System.out.print("},\n\t{");
		for (int j = 0; j <= numberOfChords - 1; j++) {
			System.out.print("\t"+typeNamesInt[types[j]-1]+", ");
		}
		System.out.print("},\n\t{");
		for (int j = 0; j < numberOfChords - 1; j++) {
			int chordDuration = (beats[j + 1] - beats[j]) * meterNumerator
					/ meterDenominator;
			System.out.print("\t"+chordDuration*48+", ");
		}
		System.out.print("\t"+meterNumerator*48+", ");
		System.out.print("}\n};\n\npublic int key = "+songKeyInt);
		
		
		
	}
	
	@SuppressWarnings("deprecation")
	public static void printMatrices() throws IOException
	{
		FileDialog fd;
		Frame f = new Frame();

		// open a MIDI file
		fd = new FileDialog(f, "Open MIDI file or choose cancel to"
				+ " finish.", FileDialog.LOAD);
		fd.show();
		// break out when user presses cancel
		if (fd.getFile() != null) {
			String filename = fd.getDirectory() + fd.getFile();
			ReadBiaBFile(filename);
		}
//		System.out.print("\t\t");
//		for (int j = 0; j <= numberOfChords - 1; j++) {
//			System.out.print(j+", \t");
//		}		
		System.out.print("\npublic int[] chordRoots = {");
		for (int j = 0; j <= numberOfChords - 1; j++) {
			System.out.print("\t"+((int)(roots[j]%18)-1)+", ");
		}
		System.out.print("};\npublic int[] chordQualities = {");
		for (int j = 0; j <= numberOfChords - 1; j++) {
			System.out.print("\t"+typeNamesInt[types[j]-1]+", ");
		}
		System.out.print("};\npublic int[] chordDurations = {");
		for (int j = 0; j < numberOfChords - 1; j++) {
			int chordDuration = (beats[j + 1] - beats[j]) * meterNumerator
					/ meterDenominator;
			System.out.print("\t"+chordDuration*48+", ");
		}
		System.out.print("\t"+meterNumerator*48+", ");
		System.out.print("};\n\npublic int key = "+songKey);
		
		
		
	}
	
	@SuppressWarnings("deprecation")
	public static void analyzeSong() throws IOException
	{
		FileDialog fd;
		Frame f = new Frame();

		// open a MIDI file
		fd = new FileDialog(f, "Open MIDI file or choose cancel to"
				+ " finish.", FileDialog.LOAD);
		fd.show();
		// break out when user presses cancel
		if (fd.getFile() != null) {
			String filename = fd.getDirectory() + fd.getFile();
			ReadBiaBFile(filename);
		}
		System.out.println("key = "+songKeyInt);
		Main.stats.key=songKeyInt;
		System.out.println("meter = "+meterNumerator);
		Main.stats.meter=meterNumerator;
		System.out.println("isMinor = "+isMinor);
		Main.stats.isMinor=isMinor;
		Main.stats.chordRoots=new int[numberOfChords];
		int rootsIndex=0;
		for (int j = 0; j <= numberOfChords - 1; j++) {
			System.out.print((int)(roots[rootsIndex]%18)-1+",\t");
			Main.stats.chordRoots[j]=(int)(roots[rootsIndex]%18)-1;
			int chordDuration = (beats[rootsIndex + 1] - beats[rootsIndex]) * meterNumerator
			/ meterDenominator;
			chordDuration=chordDuration*48;
			while (chordDuration>192)
			{
				int[] temp = Main.stats.chordRoots;
				Main.stats.chordRoots = new int[Main.stats.chordRoots.length+1];
				System.arraycopy(temp, 0, Main.stats.chordRoots, 0, temp.length);
				numberOfChords++;
				j++;
				Main.stats.chordRoots[j]=Main.stats.chordRoots[j-1];
				System.out.print(Main.stats.chordRoots[j]+",\t");
				chordDuration=chordDuration-192;
			}
			rootsIndex++;
		}
		System.out.println();
		int qualitiesIndex=0;
		Main.stats.chordQualities=new int[numberOfChords];
		for (int j = 0; j <= numberOfChords - 1; j++) {
//			System.out.println("Index = "+(types[qualitiesIndex]-1));
			System.out.print(typeNamesInt[types[qualitiesIndex]-1]+",\t");
			Main.stats.chordQualities[j]=(Integer) typeNamesInt[types[qualitiesIndex]-1];
			int chordDuration = (beats[qualitiesIndex + 1] - beats[qualitiesIndex]) * meterNumerator
			/ meterDenominator;
			chordDuration=chordDuration*48;
			while (chordDuration>192)
			{
				j++;
				Main.stats.chordQualities[j]=Main.stats.chordQualities[j-1];
				System.out.print(Main.stats.chordQualities[j]+",\t");
				chordDuration=chordDuration-192;
			}
			qualitiesIndex++;
		}
		Main.stats.chordDurations=new int[numberOfChords];
		int beatIndex=0;
		System.out.println();
		for (int j = 0; j < numberOfChords - 1; j++) {
			int chordDuration = (beats[beatIndex + 1] - beats[beatIndex]) * meterNumerator
			/ meterDenominator;
			if (chordDuration*48>=192)
			{
				Main.stats.chordDurations[j]=192;
				System.out.print(192+",\t");
			}
			else
			{
//				System.out.println("DURATION LESS THAN 192 = "+chordDuration);
				Main.stats.chordDurations[j]=(chordDuration*48);
				System.out.print((chordDuration*48)+",\t");
			}
			chordDuration=chordDuration*48;
			while (chordDuration>192)
			{
				j++;
				if (chordDuration>=192)
				{
					Main.stats.chordDurations[j]=192;
					System.out.print(192+",\t");
				}
				else
				{
					
//					System.out.println("DURATION LESS THAN 192 = "+chordDuration);
					Main.stats.chordDurations[j]=(chordDuration);
					System.out.print((chordDuration)+",\t");
				}
				chordDuration=chordDuration-192;
			}
			beatIndex++;
		}
		System.out.print((meterNumerator*48)+",\t");
		Main.stats.chordDurations[numberOfChords-1]=meterNumerator*48;

	}
}
